package com.thd.springboottest.security.securitycustom;

import com.thd.springboottest.security.utils.R;
import com.thd.springboottest.security.utils.ServletUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义登录失败后的处理器
 **/
@Component("myAuthenticationFailureHandler")
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        System.out.println("登录失败后处理");
        System.out.println("原因：" + e.getMessage());

//        httpServletResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
//        httpServletResponse.setContentType("application/json;charset=UTF-8");
//        httpServletResponse.getWriter().write("login failure:" + e.getMessage());

        ServletUtils.getInstance().failure(httpServletResponse, R.failure(" Login Failure :" + e.getMessage()));

    }
}
